package com.handpay.nocardpay.baselibrary.main.all.model.bean.component;

/**
 * Created by lyang on 2017/4/10.
 */


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;


public class ComponentMappingItemHandler extends DefaultHandler {
    private ComponentMappingItemBean mComponentMappingItem;
    private ArrayList<ComponentMappingItemBean> mIconItemList;

    final int CHANNEL = 1;
    final int CLIENT_CHANNEL = 2;
    final int ITEM_CODE = 3;
    final int ORDER_NUM = 4;
    final int SHOW_AREA = 5;
    final int UPDATE_TIME = 6;

    int currentstate = 0;


    public ComponentMappingItemHandler() {
    }


    @Override
    public void startDocument() throws SAXException {
        mIconItemList = new ArrayList<ComponentMappingItemBean>();
    }

    public ArrayList<ComponentMappingItemBean> getComponentMappingList() {
        return mIconItemList;
    }

    @Override
    public void endDocument() throws SAXException {

    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        if (localName.equals("item")) {
            mComponentMappingItem = new ComponentMappingItemBean();
            return;
        }


        if (localName.equals("CHANNEL")) {
            currentstate = CHANNEL;
            return;
        }
        if (localName.equals("CLIENT_CHANNEL")) {
            currentstate = CLIENT_CHANNEL;
            return;
        }
        if (localName.equals("ITEM_CODE")) {
            currentstate = ITEM_CODE;
            return;
        }
        if (localName.equals("ORDER_NUM")) {
            currentstate = ORDER_NUM;
            return;
        }
        if (localName.equals("SHOW_AREA")) {
            currentstate = SHOW_AREA;
            return;
        }
        if (localName.equals("UPDATE_TIME")) {
            currentstate = UPDATE_TIME;
            return;
        }
        currentstate = 0;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equals("item")) {
            mIconItemList.add(mComponentMappingItem);
            return;
        }
    }


    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String theString = new String(ch, start, length);
        switch (currentstate) {
            case CHANNEL:
                mComponentMappingItem.setChannel(theString);
                currentstate = 0;
                break;
            case CLIENT_CHANNEL:
                mComponentMappingItem.setClientChannel(theString);
                currentstate = 0;
                break;
            case ITEM_CODE:
                mComponentMappingItem.setItemCode(theString);
                currentstate = 0;
                break;
            case ORDER_NUM:
                mComponentMappingItem.setOrderNum(Integer.valueOf(theString).intValue());
                currentstate = 0;
                break;
            case SHOW_AREA:
                mComponentMappingItem.setShowArea(theString);
                currentstate = 0;
                break;
            case UPDATE_TIME:
                mComponentMappingItem.setUpdateTime(theString);
                currentstate = 0;
                break;
            default:
                return;
        }
    }
}